// apply PNG background
var myOpacityObject = function (divTarget, strPath){
	this.path = strPath + ".png";
	this.layerObject = divTarget;
	this.setBackground = function(){
		if(browser.isIE && browser.versionMinor < 7) this.layerObject.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + this.path + '", sizingMethod="scale")';
		else this.layerObject.style.backgroundImage = 'url(' + this.path + ')';
	};
};

// sets myOpacityObject right away
var alphaBg = function(tg, img){ return new myOpacityObject(tg, img).setBackground(); };

// apply PNG background to all elements with bg attribute
var setBgChilds = function(tg){
	if(typeof(tg) == "string") tg = obj(tg);
	// set self
	if(typeof(tg.getAttribute("bg")) != "undefined") alphaBg(tg, tg.getAttribute("bg"));
	// set childs
	var nodes = getElementsByAttribute(tg, "bg");
	for(var t=0; t<nodes.length; t++){ alphaBg(nodes[t], nodes[t].getAttribute("bg")); };
};

// alpha background with obj
var get2SetBg = function(idName, bgUrl){
	var thing = obj(idName);
	var tmp = new myOpacityObject(thing, bgUrl).setBackground();
	return thing;
};

// map png images from images array
function updatePng(){
	for(var t in pngLib){
		var tgs = getByClassname(t);
		for(var y in tgs){
			var iAm = tgs[y];			
			var objMyImg = new myOpacityObject(iAm, pngLib[t]);
			objMyImg.setBackground();
		}
	}
};

// find all existing rct attributes in document
var setRctChilds = function(tg, centerBg){
	if(typeof(centerBg) == "undefined") centerBg = true;
	if(typeof(tg) == "string") tg = obj(tg);
	// set self
	if(typeof(tg.getAttribute("rct")) != "undefined") setRct(tg, centerBg);
	// set childs
	var nodes = getElementsByAttribute(tg, "rct");
	for(var t=0; t<nodes.length; t++){ setRct(nodes[t], centerBg); };
};

// render png images frame around html object
var setRct = function(tg, centerBg){
	if(typeof(tg) == "string") tg = obj(tg);
	var rctPath = tg.getAttribute('rct');
	var rctStroke = tg.getAttribute('rctstroke');
	var oldPath = tg.getAttribute('rctprev');
	if(rctPath && rctStroke){
		var tW = tg.offsetWidth - 1; if(tW < 0) tW = 0;
		var tH = tg.offsetHeight;
		var rctStroke = rctStroke.split(',');
		var strokeL = rctStroke[0];
		var strokeR = rctStroke[1];
		var strokeT = rctStroke[2];
		var strokeB = rctStroke[3];
		// if new rct generate bg divs
		if(rctPath != oldPath){
			// first delete existing rcts
			var chds = xPath(tg, "div/@rctype");
			for(var l in chds) tg.removeChild(chds[l]);
			// build bg divs
			if(strokeL>0 && strokeT>0) var tl = docNode(tg, 'div', 'insert', 'before', 'rctype', 'tl', 'style', 'width:' + strokeL + 'px; height:' + strokeT + 'px; top:' + (0 - strokeT) + 'px; left:' + (0 - strokeL) + 'px; position:absolute');
			if(strokeT>0) var tm = docNode(tg, 'div', 'insert', 'before', 'rctype', 'tm', 'style', 'width:' + tW + 'px; height:' + strokeT + 'px; top:' + (0 - strokeT) + 'px; left:' + 0 + 'px; position:absolute');
			if(strokeR>0 && strokeT>0) var tr = docNode(tg, 'div', 'insert', 'before', 'rctype', 'tr', 'style', 'width:' + strokeR + 'px; height:' + strokeT + 'px; top:' + (0 - strokeT) + 'px; left:' + tW + 'px; position:absolute');
			if(strokeL>0) var ml = docNode(tg, 'div', 'insert', 'before', 'rctype', 'ml', 'style', 'width:' + strokeL + 'px; height:' + tH + 'px; top:' + 0 + 'px; left:' + (0 - strokeL) + 'px; position:absolute');
			if(strokeR>0) var mr = docNode(tg, 'div', 'insert', 'before', 'rctype', 'mr', 'style', 'width:' + strokeR + 'px; height:' + tH + 'px; top:' + 0 + 'px; left:' + tW + 'px; position:absolute');
			if(strokeL>0 && strokeB>0) var bl = docNode(tg, 'div', 'insert', 'before', 'rctype', 'bl', 'style', 'width:' + strokeL + 'px; height:' + strokeB + 'px; top:' + tH + 'px; left:' + (0 - strokeL) + 'px; position:absolute');
			if(strokeB>0) var bm = docNode(tg, 'div', 'insert', 'before', 'rctype', 'bm', 'style', 'width:' + tW + 'px; height:' + strokeB + 'px; top:' + tH + 'px; left:' + 0 + 'px; position:absolute');
			if(strokeR>0 && strokeB>0) var br = docNode(tg, 'div', 'insert', 'before', 'rctype', 'br', 'style', 'width:' + strokeR + 'px; height:' + strokeB + 'px; top:' + tH + 'px; left:' + tW + 'px; position:absolute');
			// set background imgs
			var eml = tg.getElementsByTagName("div");
			for(var t=0; t<eml.length; t++){
				if(eml[t].getAttribute("rctype")){
					eml[t].style.fontSize = "0px";
					alphaBg(eml[t], rctPath + '/' + eml[t].getAttribute('rctype'));
				}
			}
			// set center background
			if(centerBg == true) tg.style.backgroundImage = "url(" + rctPath + "/mm.png)";
			// remember previous path
			tg.setAttribute('rctprev', rctPath);
		}
		// else justmove elements to new positions
		else{
			var eml = xPath(tg, "div[@rctype]");
			for(var t in eml){
				var em = eml[t];
				switch(em.getAttribute('rctype')){
					case 'tm': em.style.width = tW + 'px'; break;
					case 'tr': em.style.left = tW + 'px'; break;
					case 'ml': em.style.height = tH + 'px'; break;
					case 'mr': em.style.height = tH + 'px'; em.style.left = tW + 'px'; break;
					case 'bl': em.style.top = tH + 'px'; break;
					case 'bm': em.style.top = tH + 'px'; em.style.width = tW + 'px'; break;
					case 'br': em.style.top = tH + 'px'; em.style.left = tW + 'px'; break;
				}
			}
		}
	}
};
